package com.haliyoo.adhere.bean;

import android.util.Log;

/**
 * 数据包
 * @author wation.haliyoo
 *
 */

public class PacketBean {

	private static final String TAG = "PacketParser";

	/**
	 * 常量
	 */
	// /项目名
	private static final String projectName = "";
	private static final String adProject = "adhs";
	// /模块名
	private static final String moduleName = "";
	private static final String userModule = "user";
	private static final String adModule = "ad";
	// /子模块名
	private static final String subModuleName = "";
	// /类名
	private static final String className = "";
	private static final String loginClass = "login";
	private static final String userClass = "user";
	private static final String adClass = "ad";
	// /方法名
	private static final String methodName = "";
	private static final String loginMethod = "login";
	private static final String logoutMethod = "logout";
	private static final String registerMethod = "register";
	private static final String updateMethod = "update";
	private static final String postMethod = "ads.aspx";
	private static final String delMethod = "del";
	private static final String repostMethod = "ads.aspx";
	private static final String listMethod = "GetadsList.aspx";
	private static final String countMethod = "GetDevoted.aspx";

	/**
	 * 组一个请求包 例如：
	 * http://serverAddr/Project/ModuleName/SubModuleName/ClassName!methodName
	 * ?arg1=xxx&arg2=xxx
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param project
	 *            项目名
	 * @param moduleName
	 *            模块名
	 * @param subModuleName
	 *            子模块名
	 * @param className
	 *            类名
	 * @param methodName
	 *            方法名
	 * @param argKeyValues
	 *            键值对集合
	 * @return 数据包
	 */
	public static String assembleRequest(String serverAddr, String project,
			String moduleName, String subModuleName, String className,
			String methodName, String argKeyValues) {
		String retStr = null;

		// /HTTP头部
		retStr = "http://";

		// /服务器地址
		if (serverAddr == null || serverAddr.length() <= 0) {
			return null;
		} else {
			retStr += serverAddr + "/";
		}

		// /项目名
		if (project == null || project.length() <= 0) {
		} else {
			retStr += project + "/";
		}

		// /模块名
		if (moduleName == null || moduleName.length() <= 0) {
		} else {
			retStr += moduleName + "/";
		}

		// /子模块名
		if (subModuleName == null || subModuleName.length() <= 0) {
		} else {
			retStr += subModuleName + "/";
		}

		// /类名
		if (className == null || className.length() <= 0) {
		} else {
			retStr += className + "!";
		}

		// /方法名
		if (methodName == null || methodName.length() <= 0) {
			return null;
		} else {
			retStr += methodName + "?";
		}

		// /键值对集合
		retStr += argKeyValues;

		Log.i(TAG, "request: " + retStr);

		return retStr;
	}

	/**
	 * 组一个登录包 例如：
	 * http://ServerAddr/adhs/user/login!login?userId=XXX&password=XXX
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param password
	 *            密码
	 * @return 数据包
	 */
	public static String assembleLoginRequest(String serverAddr, String userId,
			String password) {
		String argKeyValues = "weiboUserId=" + userId + "&" + "password=" + password;

		return assembleRequest(serverAddr, adProject, moduleName,
				subModuleName, className, loginMethod, argKeyValues);
	}

	/**
	 * 组一个退出包 例如： http://ServerAddr/adhs/user/login!logout?userId=XXX&sign=XXXX
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param sign
	 *            签名
	 * @return 数据包
	 */
	public static String assembleLogoutRequest(String serverAddr,
			String userId, String sign) {
		String argKeyValues = null;
		
		if (sign == null) {
			argKeyValues = "userId=" + userId;
		} else {
			argKeyValues = "userId=" + userId + "&" + "sign=" + sign;
		}

		return assembleRequest(serverAddr, adProject, moduleName,
				subModuleName, loginClass, logoutMethod, argKeyValues);
	}

	/**
	 * 组一个注册包 例如：
	 * http://ServerAddr/adhs/user/user!register?userId=XXX&password=XXX
	 * &username=XXX
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param password
	 *            密码
	 * @param userName
	 *            用户名
	 * @return 数据包
	 */
	public static String assembleRegisterRequest(String serverAddr,
			String userId, String password, String userName) {
		String argKeyValues = "userId=" + userId + "&" + "password=" + password
				+ "&" + "username=" + userName;

		return assembleRequest(serverAddr, adProject, moduleName,
				subModuleName, userClass, registerMethod, argKeyValues);
	}

	/**
	 * 组一个更新用户信息的数据包 例如：
	 * http://ServerAddr/adhs/user/user!update?userId=XXX&username
	 * =XXXX&sign=XXXX
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param userName
	 *            用户名
	 * @param sign
	 *            签名
	 * @return 数据包
	 */
	public static String assembleUpdateInfoRequest(String serverAddr,
			String userId, String userName, String sign) {
		String argKeyValues = null;
		
		if (sign == null) {
			argKeyValues = "userId=" + userId + "&" + "username=" + userName;
		} else {
			argKeyValues = "userId=" + userId + "&" + "username=" + userName
			+ "&" + "sign=" + sign;
		}

		return assembleRequest(serverAddr, adProject, moduleName,
				subModuleName, userClass, updateMethod, argKeyValues);
	}

	/**
	 * 组一个修改密码的数据包 例如：
	 * http://ServerAddr/adhs/user/user!update?userId=XXX&password
	 * =XXX&newpassword=XXX&sign=XXXX
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param password
	 *            密码
	 * @param newPassword
	 *            新密码
	 * @param sign
	 *            签名
	 * @return 数据包
	 */
	public static String assembleUpdatePasswordRequest(String serverAddr,
			String userId, String password, String newPassword, String sign) {
		String argKeyValues = null;
		
		if (sign == null) {
			argKeyValues = "userId=" + userId + "&" + "password=" + password
			+ "&" + "newpassword=" + newPassword;
		} else {
			argKeyValues = "userId=" + userId + "&" + "password=" + password
			+ "&" + "newpassword=" + newPassword + "&" + "sign=" + sign;
		}

		return assembleRequest(serverAddr, adProject, moduleName,
				subModuleName, userClass, updateMethod, argKeyValues);
	}

	/**
	 * 组一个发布包 例如：
	 * http://ServerAddr/adhs/ad/ad!post?userId=XXX&adid=XXX&sign=XXXX
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param adId
	 *            广告ID
	 * @param sign
	 *            签名
	 * @return 数据包
	 */
	public static String assemblePostRequest(String serverAddr, String userId,
			String adId, String sign) {
		String argKeyValues = null;
		
		if (sign == null) {
			argKeyValues = "userId=" + userId + "&" + "Sendadid=" + adId;
		} else {
			argKeyValues = "userId=" + userId + "&" + "Sendadid=" + adId
			+ "&" + "sign=" + sign;
		}

		return assembleRequest(serverAddr, adProject, moduleName, subModuleName,
				className, postMethod, argKeyValues);
	}

	/**
	 * 组一个删除包 例如： http://ServerAddr/adhs/ad/ad!del?userId=XXX&adid=XXX&sign=XXXX
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param adId
	 *            广告ID
	 * @param sign
	 *            签名
	 * @return 数据包
	 */
	public static String assembleDeleteRequest(String serverAddr,
			String userId, String adId, String sign) {
		String argKeyValues = null;
		
		if (sign == null) {
			argKeyValues = "userId=" + userId + "&" + "adId=" + adId;
		} else {
			argKeyValues = "userId=" + userId + "&" + "adId=" + adId
			+ "&" + "sign=" + sign;
		}

		return assembleRequest(serverAddr, adProject, moduleName, subModuleName,
				adClass, delMethod, argKeyValues);
	}

	/**
	 * 组一个转发包 例如：
	 * http://ServerAddr/adhs/ad/ad!repost?userId=XXX&adid=XXX&sign=XXXX
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param sign
	 *            签名
	 * @return 数据包
	 */
	public static String assembleRepostRequest(String serverAddr,
			String userId, String sign) {
		String argKeyValues = null;
		
		if (sign == null) {
			argKeyValues = "userId=" + userId;
		} else {
			argKeyValues = "userId=" + userId + "&" + "sign=" + sign;
		}

		return assembleRequest(serverAddr, adProject, moduleName, subModuleName,
				className, repostMethod, argKeyValues);
	}

	/**
	 * 组一个转发包 例如：
	 * http://ServerAddr/adhs/ad/ad!repost?userId=XXX&adid=XXX&sign=XXXX
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param sign
	 *            签名
	 * @return 数据包
	 */
	public static String assembleRepostRequest(String serverAddr,
			String userId, String sign, String[] adIds) {
		String argKeyValues = null;
		String adIdParams = adIds[0];
		
		for (int i=1; i<adIds.length; i++) {
			adIdParams += "," + adIds[i];
		}
		
		if (sign == null) {
			argKeyValues = "userId=" + userId 
			+ "&" + "adIds=" + adIdParams;
		} else {
			argKeyValues = "userId=" + userId + "&" + "sign=" + sign
			+ "&" + "adIds=" + adIdParams;
		}

		return assembleRequest(serverAddr, adProject, moduleName, subModuleName,
				className, repostMethod, argKeyValues);
	}
	
	/**
	 * 组一个获取广告列表的数据包 例如： http://ServerAddr/adhs/ad/ad!list?userId=XXX&sign=XXXX
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param sign
	 *            签名
	 * @return 数据包
	 */
	public static String assembleListRequest(String serverAddr, String userId,
			String sign) {
		String argKeyValues = null;
		
		if (sign == null) {
			argKeyValues = "userId=" + userId;
		} else {
			argKeyValues = "userId=" + userId + "&" + "sign=" + sign;
		}

		return assembleRequest(serverAddr, adProject, moduleName, subModuleName,
				className, listMethod, argKeyValues);
	}

	/**
	 * 组一个转发包的内容
	 * 
	 * @param adIds
	 *            广告ID集合
	 * @return 数据包内容
	 */
	public static String assembleRepostContent(String[] adIds) {
		String retXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		
		for (int i=0; i<adIds.length; i++) {
			retXml += adIds + ",";
		}


		return retXml;
	}

	/**
	 * 组一个获取数目包的内容
	 * 
	 * @param userId
	 *            用户ID
	 * @param sign
	 *            签名
	 * @param customerId
	 *            待查用户id
	 * @return 数据包内容
	 */
	public static String assembleCountRequest(String serverAddr, String userId,
			String sign, String customerId) {
		String argKeyValues = null;
		
		if (sign == null) {
			argKeyValues = "userId=" + userId + "&"
			+ "merId=" + customerId;
		} else {
			argKeyValues = "userId=" + userId + "&" + "sign=" + sign + "&"
			+ "merId=" + customerId;
		}

		return assembleRequest(serverAddr, adProject, moduleName, subModuleName,
				className, countMethod, argKeyValues);
	}
}
